package com.lhf.novel.sysuser.controller;

import com.github.pagehelper.PageInfo;
import com.lhf.novel.sysuser.entity.SysUser;
import com.lhf.novel.sysuser.service.SysUserService;
import com.lhf.novel.sysuser.vo.SysUserInfoVo;
import com.lhf.novel.sysuser.vo.SysUserVo;
import com.lhf.novel.util.ExceptionCode;
import com.lhf.novel.util.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制层
 *
 * @author 刘会发
 * @since 2020-03-09 11:19:26
 */
@RestController
@RequestMapping("sysUser")
@Api(tags = "用户操作控制层")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    @GetMapping("query-all")
    @ApiOperation(value = "查询所有的用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataTypeClass = String.class, example = "admin"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", dataTypeClass = String.class, example = "admin"),
            @ApiImplicitParam(name = "email", value = "电子邮件", dataTypeClass = String.class, example = "admin"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataTypeClass = String.class, example = "admin"),
            @ApiImplicitParam(name = "isLock", value = "是否锁定", dataTypeClass = Integer.class, example = "0"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataTypeClass = Integer.class, example = "1", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示数量", dataTypeClass = Integer.class, example = "10", defaultValue = "10")
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限"),
    })
    public Result<PageInfo<SysUserVo>> queryAll(@RequestParam(required = false) String username,
                                                @RequestParam(required = false) String realName,
                                                @RequestParam(required = false) String email,
                                                @RequestParam(required = false) String phone,
                                                @RequestParam(required = false) Integer isLock,
                                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(sysUserService.queryAll(username, realName, email, phone, isLock, pageNum, pageSize));
    }

    @PostMapping("add")
    @ApiOperation("添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataTypeClass = String.class, example = "admin", required = true),
            @ApiImplicitParam(name = "realName", value = "真实姓名", dataTypeClass = String.class, example = "张三", required = true),
            @ApiImplicitParam(name = "email", value = "电子邮件", dataTypeClass = String.class, example = "wsdsg@qq.com", required = true),
            @ApiImplicitParam(name = "phone", value = "手机号", dataTypeClass = String.class, example = "13091323232", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataTypeClass = String.class, example = "admin"),
            @ApiImplicitParam(name = "roles", value = "角色数组的json字符串", dataTypeClass = String.class, example = "[1,2,3,4,5,6,7]"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<Boolean> insert(String username,
                                  String realName,
                                  String email,
                                  String phone,
                                  String password,
                                  @RequestParam(required = false) String roles) throws MessagingException {
        return Result.success(sysUserService.insert(username, realName, email, phone, 1, password, roles));
    }

    @GetMapping("activate")
    @ApiOperation("激活账户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, example = "1", defaultValue = "1")
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常")
    })
    @ApiIgnore
    public void activate(Integer id) throws IllegalAccessException {
        sysUserService.activate(id);
    }

    @PatchMapping("update-password")
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldVal", value = "用户旧密码", dataTypeClass = String.class, example = "123456", required = true),
            @ApiImplicitParam(name = "newVal", value = "用户的新密码", dataTypeClass = String.class, example = "456789", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常")
    })
    public Result<Integer> updatePassword(String oldVal, String newVal, HttpServletRequest request) throws IllegalAccessException {
        if (sysUserService.updatePassword(oldVal, newVal, request) == 0) {
            return Result.other(ExceptionCode.PASSWORD_ERROR.getCode(), null, ExceptionCode.PASSWORD_ERROR.getMes());
        } else {
            //修改成功
            return Result.success(null);
        }
    }


    @PostMapping("authorization")
    @ApiOperation("用户授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataTypeClass = Integer.class, example = "1", required = true),
            @ApiImplicitParam(name = "jsonRoles", value = "角色的json数组", dataTypeClass = String.class, example = "[1,2,3]", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限"),
    })
    public Result<Integer> authorization(Integer uid, String jsonRoles) {
        return Result.success(sysUserService.authorization(uid, jsonRoles));
    }

    @GetMapping("info")
    @ApiOperation("查询用户详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户的id", dataTypeClass = Integer.class, example = "1"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<SysUserInfoVo> queryById(Integer uid) {
        return Result.success(sysUserService.queryById(uid));
    }

    @PutMapping("update")
    @ApiOperation("修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "realName", value = "真实用户名", dataTypeClass = String.class, example = "张三"),
            @ApiImplicitParam(name = "headImg", value = "头像", dataTypeClass = String.class, example = "http://123456.jpj"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataTypeClass = String.class, example = "13091326588"),
            @ApiImplicitParam(name = "isLock", value = "是否锁定 0未锁定 1锁定中", dataTypeClass = Integer.class, example = "0"),
            @ApiImplicitParam(name = "email", value = "电子邮件", dataTypeClass = String.class, example = "zhubajie@qq.com"),
            @ApiImplicitParam(name = "id", value = "用户id", dataTypeClass = Integer.class, example = "1", required = true),
            @ApiImplicitParam(name = "id", value = "用户id", dataTypeClass = Integer.class, example = "[1,2,3,4]", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<SysUserInfoVo> update(@RequestParam(required = false) String realName,
                                        @RequestParam(required = false) String headImg,
                                        @RequestParam(required = false) String phone,
                                        @RequestParam(required = false) Integer isLock,
                                        @RequestParam(required = false) String email,
                                        @RequestParam(required = false) String ridJson,
                                        @RequestParam Integer id) throws IllegalAccessException {
        return Result.success(sysUserService.update(new SysUser(id, realName, email, headImg, phone, isLock), ridJson));
    }

    @PatchMapping("lock")
    @ApiOperation("修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isLock", value = "是否锁定 0未锁定 1锁定中", dataTypeClass = Integer.class, example = "0"),
            @ApiImplicitParam(name = "id", value = "用户id", dataTypeClass = Integer.class, example = "1", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<SysUserInfoVo> update(@RequestParam(required = false) Integer isLock,
                                        @RequestParam Integer id) throws IllegalAccessException {
        return Result.success(sysUserService.update(new SysUser(id, null, null, null, null, isLock), null));
    }

    @DeleteMapping("delete")
    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户的id", dataTypeClass = Integer.class, example = "20"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<SysUserInfoVo> del(Integer uid) throws IllegalAccessException {
        SysUser sysUser = new SysUser();
        sysUser.setId(uid);
        sysUser.setIsDel(1);
        return Result.success(sysUserService.update(sysUser, null));
    }

    @GetMapping("isExit")
    @ApiOperation("查询用户名是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataTypeClass = String.class, example = "admin"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<Boolean> isExistForUsername(String username) {
        return Result.success(sysUserService.isExistForUsername(username));
    }
}